import { Behaviour, getGameObjectById, HitTestScript } from "../gameengine";
import { sentence, Conversation } from "./conversation";
import {
    ConversationKeyClueState,
    ConversationMachine,
    ConversationNormalState,
} from "./conversationMachine";
import { ColliderSystem } from "../collider/colliderSystem";
import { user } from "../user";
import {npcDialogClueDictionary, npcDialogNormalDictionary} from "./conversationContent";

const testSentence = [
    {
        name: "岡山雄二",
        content: "本书讲述了一个中国人的故事，以及关于他的种种谎言。"
    }, {
        name: "岡山雄二",
        content: "我不知道他现在还活着吗？"
    }, {
        name: "岡山雄二",
        content: "我希望本书能让他继续存在。"
    }, {
        name: "岡山雄二",
        content: "也希望本书能为诸君所喜欢。"
    }
];

const testSentence2 = [
    {
        name: "冈山爱",
        content: "他因谎言实现人生价值"
    }, {
        name: "冈山爱",
        content: "亦因谎言落入深渊"
    }, {
        name: "冈山爱",
        content: "前半生靠自己的谎言救回了别人"
    }, {
        name: "冈山爱",
        content: "后半人靠别人的谎言救回了自己。"
    }
];

const testSentence3 = [
    {
        name: "张晓梦",
        content: "约定还在"
    }, {
        name: "张晓梦",
        content: "敬请期待"
    }
];

const d1 = [
    {
        name: "方别",
        content: "欸？人怎么没了。刚刚他说，‘猫儿闹，鱼儿叫，珍珠在睡觉，蓝色的血，在管道深处，人们都需要’，这什么意思啊.."
    }, {
        name: "方别",
        content: "hey"
    }
];

const d2 = [
    {
        name: "LeeYushan",
        content: "海蛇"
    }, {
        name: "方别",
        content: "我是海蛇"
    }
];


const clues: sentence[][] = [testSentence, testSentence2, testSentence3];
const daily: sentence[][] = [d1, d2];

type ConversationState = "none" | "normal" | "keyClue";

export class ConversationTest extends Behaviour {

    public conversation: Conversation;
    private conversationMachine: ConversationMachine;

    public state: ConversationState = 'none';

    protected onStart() {
        //Conversation使用办法:
        //首先new一个Conversation
        //指定normalDialoga和keyCluesDialogs

        console.log('加载对话框');
        console.log("item状态为：", this.state);
        this.conversation = new Conversation();
        this.conversation.displayObject=this.displayObject;

        this.conversationMachine = this.conversation.conversationMachine;
        // this.conversationMachine.normalDialogs = daily;
        // this.conversationMachine.keyClueDialogs = clues;

        this.conversationMachine.normalDialogs=npcDialogNormalDictionary[this.displayObject.id];
        this.conversationMachine.keyClueDialogs=npcDialogClueDictionary[this.displayObject.id];
    }

    searchKeys(cluekey: string) {
        console.log("search keys");
        // if (user.keys.length < 1) {
        //     console.log("未获取相应线索");
        //     this.conversationMachine.state="normal";
        //     this.ChangeState('normal');
        // }

        var finded=false;
        for (let key of user.keys) {
            if (key == cluekey) {
                console.log("已获取相应线索");
                finded=true;
                this.conversationMachine.state="keyClue";
                this.ChangeState('keyClue');
                return;
            }
        }

        if(!finded){
            this.conversationMachine.state="normal";
            this.ChangeState('normal');
        }
        
    }

    ChangeState(state: ConversationState) {
        // if (this.state != state) {
        //     this.state = state;
        //     console.log(this.state);
        //     this.onStateChanged(this.state);
        // }
        this.onStateChanged(this.state);
    }

    private clearColliders(){
        const itemColliders = getGameObjectById("Scene").getScript(ColliderSystem).itemColliders;
        if (itemColliders.length < 1) {
            return;
        }
        for (let itemCollider of itemColliders) {
            itemColliders.pop();
        }
    }

    onStateChanged(state: ConversationState) {

        if(ConversationMachine.istalking){
            console.log("正在对话中！不要切换状态");
            return;
        }
        console.log("切换状态");
        console.log(this.conversationMachine.state);
        // console.log("状态切换为：", state);
        //
        // if (state == "none") {
        //     const itemColliders = getGameObjectById("Scene").getScript(ColliderSystem).itemColliders;
        //     if (itemColliders.length < 1) {
        //         return;
        //     }
        //     for (let itemCollider of itemColliders) {
        //         itemColliders.pop();
        //     }
        // }

        //常规对话
        if (this.conversationMachine.state == "normal") {

            const normalState = new ConversationNormalState(this.conversationMachine);
            this.conversationMachine.changeState(normalState);
            this.clearColliders();
        }
        //线索对话
        if (this.conversationMachine.state == "keyClue") {
            const keyClueState = new ConversationKeyClueState(this.conversationMachine);
            this.conversationMachine.changeState(keyClueState);
            this.clearColliders();
            // this.conversationMachine.state="normal";
        }

    }

    protected onUpdate(advancedTime: number) {
    }

}